<?php

namespace App\Http\Controllers\Customer;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Http\Validate\Customer\address;
use App\Models\Customer\AddressModel;
use App\Service\Balance\BalanceService;
use App\Service\Customer\AddressService;
use App\Service\Customer\CustomerRelateService;
use Illuminate\Http\Request;
use App\Service\Customer\CustomerService;

class CustomerRelateController extends Controller
{
    public function followUser(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        Result::isEmpty($request->follow_uid,"关注用户不能为空");

        $data = array(
            'uid' => $request->uid,
            'fuid' => $request->follow_uid,
            'created_at' => date("Y-m-d H:i:s"),
        );
		
		if($request->uid == $request->follow_uid){
            Result::msg('-41','error','自己不能关注自己');
        }

        //是否已经关注过
        if(CustomerRelateService::findFollow($request->uid, $request->follow_uid)){
            Result::msg('-32','error','你已经关注过此人');
        }

        if(!CustomerRelateService::addFollow($data)){
            Result::msg('-11','error','更新失败');
        }

        Result::msg('0','success',$data);

    }
	
	/**
     * 取消关注
     */
    public function cancleFollow(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        Result::isEmpty($request->follow_uid,"关注用户不能为空");

        $data = array(
            'uid' => $request->uid,
            'fuid' => $request->follow_uid,
            'created_at' => date("Y-m-d H:i:s"),
        );

        //是否已经关注过
        if(!CustomerRelateService::findFollow($request->uid, $request->follow_uid)){
            Result::msg('-33','error','你尚未关注此人');
        }

        if(!CustomerRelateService::removeFollow($request->uid,$request->follow_uid)){
            Result::msg('-11','error','更新失败');
        }

        Result::msg('0','success',$data);
    }
	
	/**
     * 获得我关注的作者文章列表
     */
    public function myFollowAuthoerArticle(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");

        if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $page = $request->input('page');

        $offest = ($page-1)*10;
        $page = $page*10;

        $works = CustomerRelateService::getFollowAuthorArticle($request->uid,$page,$offest);

        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	
	/**
     * 获得我的粉丝
     */
    public function myFans(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $page = $request->input('page');
        $offest = ($page-1)*10;
        $page = 10;

        $fans = CustomerRelateService::getUserFans($request->uid,$page,$offest);
		
        if($fans){
            Result::msg('0',"success",$fans);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	/**
     * 获得我的关注
     */
    public function myFollows(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $page = $request->input('page');
        $offest = ($page-1)*10;
        $page = 10;

        $follows = CustomerRelateService::getUserFollows($request->uid,$page,$offest);

        if($follows){
            Result::msg('0',"success",$follows);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	
	 /**
     * 我的收入总额
     */
    public function mySumIncome(Request $request)
    {
        $uid = $request->uid;
        if(!CustomerService::findUser(['uid' => $uid])){
            Result::msg('-16','error','无有效用户信息');
        }
        $incomes = OrderService::getMyIncome($uid);
        Result::msg('0',"success",$incomes);
    }
	
	/**
     * 获取推荐人
     */
    public function systemRecommendAuthor(Request $request)
    {
        Result::isEmpty($request->uid,"用户名不能为空");
        /*if(!CustomerService::findUser(['uid'=>$request->uid])){
            Result::msg('-16',"error","无有效用户信息");
        }*/
        $recommends = CustomerRelateService::getRecommendAuthors($request->uid);
        Result::msg('0',"success",$recommends);
    }

    /**
     * 设置收货地址
     * @param Request $request
     */
    public function setShippingAddress(Request $request)
    {
        $validator = address::validate($request);

        if(!empty($validator)){
            Result::msg('-1','error',$validator[0]);
        }

        $data = array(
            'uid' => $request->uid,
            'address' => $request->address,
            'name' => $request->name,
            'phone' => $request->phone,
            'province_id' => $request->province_id,
            'province' => $request->province,
            'city_id' => $request->city_id,
            'city' => $request->city,
            'code' => $request->code
        );

        if($request->area_id){
            $data['area_id'] = $request->area_id;
        }

        if($request->area){
            $data['area'] = $request->area;
        }

        //获得当前地址
        if(!AddressService::defaultAddress($request->uid)){
            $data['is_default'] = 1;
        }
        if(AddressService::addAddress($data)){
            Result::msg('0','success',$data);
        }else{
            Result::msg('-11','error','更新失败');
        }
    }

    /**
     * 编辑收货地址
     */
    public function editAddress(Request $request)
    {
        $validator = address::validate($request);

        if(!empty($validator)){
            Result::msg('-1','error',$validator[0]);
        }

        $data = array(
            'address' => $request->address,
            'name' => $request->name,
            'phone' => $request->phone,
            'province_id' => $request->province_id,
            'province' => $request->province,
            'city_id' => $request->city_id,
            'city' => $request->city
        );

        if($request->area_id){
            $data['area_id'] = $request->area_id;
        }

        if($request->area){
            $data['area'] = $request->area;
        }
        $id = $request->address_id;
        if(AddressService::changeAddress($id,$data)){
            Result::msg('0','success',$data);
        }else{
            Result::msg('-11','error','更新失败');
        }
    }

    /**
     * 删除收货地址
     */
    public function delAddress(Request $request)
    {
        $id = $request->address_id;
        if(AddressService::deleteAddress($id)){
            Result::msg('0','success',[]);
        }else{
            Result::msg('-11','error','更新失败');
        }
    }

    /**
     * 获得收货地址
     */
    public function showMyAddress(Request $request)
    {
        Result::isEmpty($uid = $request->uid,'无有效用户名称','-16');
        $id = $request->input('id',0);

        if($id>0){
            $results = AddressService::getOneAddress($id,$uid);
        }else{
            $results = AddressService::getAddress($uid);
        }


		if(!$results){
			Result::msg(700,'error','没有更多了');
		}

        Result::msg(0,'success',$results);
    }

    /**
     * 设为默认地址
     */
    public function setDefaultAddress(Request $request)
    {
        Result::isEmpty($address_id = $request->address_id,'','');
        Result::isEmpty($uid = $request->uid,'无有效用户名称','-16');

        $result = AddressService::changeAddressDefault($request->address_id,$request->uid);

        if($result){
            Result::msg('0','success',[]);
        }else{
            Result::msg('-11','error','更新失败');
        }
    }

    /**
     * 收入支出明细
     * @param Request $request
     */
    public function userBalanceLog(Request $request)
    {
        Result::isEmpty($request->uid,'无有效用户信息',-16);
        $user_info = CustomerService::findUser([
            'uid' => $request->uid
        ]);
        if(!$user_info){
            Result::msg('-16','error','无有效用户信息');
        }

        $page = $request->input('page');
        $offset = ($page-1)*10;

        $result = BalanceService::getBalanceLog($request->uid,$offset);
        if($result){
            Result::msg('0',"success",$result);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }

    /**
     * 获得省市区
     */
    public function provincesCityArea(Request $request)
    {
        $type = $request->type;
        $id = $request->id;

        $result = AddressService::getProvincesCityArea($type,$id);
        if($result){
            Result::msg('0',"success",$result);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }

    /**
     * 默认收货地址
     * @param Request $request
     */
    public function myDefaultAddress(Request $request)
    {
        Result::isEmpty($uid = $request->uid,'无有效用户名称','-16');

        $results = AddressService::defaultAddress($uid);
        if(!$results){
            $results = AddressModel::where('uid',$uid)->first();
        }
        if(!$results){
            Result::msg('-18','error','没有更多了');
        }

        Result::msg('0','success',$results);
    }
}